package date_demo;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

/**
 * @author ChenYongHao
 * @date 2023/5/4 10:20
 * @desc 时间校正器
 */


public class TemporalAdjusters_Demo {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.of(2020, Month.JULY, 21, 12, 30, 32);
        // 增加一年
        localDateTime = localDateTime.plusYears(1);
        localDateTime = localDateTime.plus(1, ChronoUnit.YEARS);
        System.out.println(localDateTime);
        System.out.println("----------");

        // 减少一年
        localDateTime = localDateTime.minusYears(1);
        localDateTime = localDateTime.minus(1, ChronoUnit.YEARS);
        System.out.println(localDateTime);
        System.out.println("----------");

        // 指定年份
        localDateTime = localDateTime.withYear(2020);
        localDateTime = localDateTime.with(ChronoField.YEAR, 2020);
        System.out.println(localDateTime);
        System.out.println("----------");

        // 获取该年的第一天
        localDateTime = localDateTime.with(TemporalAdjusters.firstDayOfYear());
        System.out.println(localDateTime);
        System.out.println("----------");

        // 自定义：下一个工作日
        localDateTime = localDateTime.with((l) -> {
            LocalDateTime ldt = (LocalDateTime) l;
            DayOfWeek dow = ldt.getDayOfWeek();
            if (dow.equals(DayOfWeek.FRIDAY)) {
                return ldt.plusDays(3);
            } else if (dow.equals(DayOfWeek.SATURDAY)) {
                return ldt.plusDays(2);
            } else {
                return ldt.plusDays(1);
            }
        });
        System.out.println(localDateTime);
        System.out.println("----------");
    }
}
